package com.ynny.project01.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.lettuce.core.resource.ClientResources;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

// redis配置类和缓存配置, 序列化配置
// connectionFactory 负责与Redis服务器的连接交互
// LettuceConnectionConfiguration解析配置
// RedisCacheConfiguration 缓存配置
// Jackson2JsonRedisSerializer 缓存序列化

// (1.)将对象序列化为json格式，并使用RedisSerializer.string()序列化key/值
@Configuration
public class RedisConfig {
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {

        ObjectMapper om = new ObjectMapper();
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(
                om, Object.class);
        // 解决查询缓存转换异常的问题
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        // 配置序列化（解决乱码的问题）
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofMillis(-1))
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
                .disableCachingNullValues();
        RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);

        return new RedisConfigCacheManager(cacheWriter, config);

    }
}
















//连接池和客户端连接
//@Configuration
//public class RedisConfig  {
//    @Bean
//    public LettuceConnectionFactory redisConnectionFactory(RedisProperties properties) {
//        RedisProperties.Lettuce lettuce = properties.getLettuce();
//        RedisProperties.Pool pool = lettuce.getPool();
//
//        LettucePoolingClientConfiguration config;
//        if (pool != null && !pool.isEnabled()) {
//            config = LettucePoolingClientConfiguration.builder().build();
//        } else {
//            config = createPoolingConfig(pool);
//        }
//
//        return new LettuceConnectionFactory(
//                configureClientOptions(lettuce.getClientOptions()),
//                config,
//                resolveRedisURI(properties));
//    }
